package lz.controls.nickScroll
{
	import flash.display.DisplayObject;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	
	import lz.controls.ScrollBar;
	import lz.controls.events.ScrollEvent;
	
	/**
	 * 滚动条 
	 * @author Administrator
	 * 
	 */	
	public class ScrollBarView extends Sprite
	{
		
		/**
		 *滚动条位置在左边 
		 */
		public static const LEFT:String = "left";
		/**
		 *滚动条位置在右边 
		 */
		public static const RIGHT:String = "right";
		private var _scrollBar:ScrollBar;
		/**
		 *目标即内容  
		 */
		private var _target:Sprite;
		/**
		 *宽 
		 */
		private var _width:int;
		/**
		 * 高
		 */
		private var _height:int;
		/**
		 *内容容器 
		 */
		private var _contain:Sprite;
		/**
		 ** <p> _scrollBar = new ScrollBarView();</p>
		 * <p>_scrollBar.setSizeView(symbolSkin.width,symbolSkin.height);设置宽高</p>
		 * <p>this.addChild(_scrollBar);</p>
		 * <p>_scrollBar.target = _target;设置内容</p>
		 * <p>_scrollBar.lineScrollSize = 10;设置滚动量</p>
		 * <p>_scrollBar.setPlace(right);设置位置，默认为右边</p>
		 * <p> _scrollBar.upData(); 更新</p> 
		 * 
		 */		
		public function ScrollBarView()
		{
			super();
			init();
		}
		private function init():void
		{
			_scrollBar = new ScrollBar();
			this.addChild(_scrollBar);
			
			_scrollBar.addEventListener(ScrollEvent.SCROLL,onHandler,false,0,true);
			this.addEventListener(MouseEvent.MOUSE_WHEEL,onWheelHandler);
		}
		protected function onHandler(event:ScrollEvent):void
		{
			_target.y = -(_target.height -_height) * (event.position /_scrollBar.maxScrollPosition );
		}
		/**
		 *鼠标滚动轴事件  
		 * @param event
		 * 
		 */
		private function onWheelHandler(event:MouseEvent):void
		{
			_scrollBar.scrollPosition -= event.delta*_scrollBar.lineScrollSize ;
			
		}
		/**
		 * 设置滚动（显示）大小 滚动宽度默认为20
		 * @param $width
		 * @param $height
		 * 
		 */
		public function setSizeView($width:int,$height:int):void
		{
			_height = $height;
			_width = $width;
			ScrollBar.WIDTH = 9
			_scrollBar.setSize(9,$height);
			_scrollBar.setScrollProperties(_height,0,20);
			_scrollBar.x = $width + 2;
			_contain = new Sprite();
			_scrollBar.lineScrollSize = 10;
			this.addChild(_contain);
			
			var shape:Shape = new Shape();
			shape.graphics.clear();
			shape.graphics.beginFill(0xFFFFFF);
			shape.graphics.drawRect(0,0,$width,$height);
			shape.graphics.endFill();
			_contain.addChild(shape);
			_contain.mask = shape;
		}
		/**
		 * 滚动条的滚动量
		 * <p>P默认为10</p> 
		 * @param $value
		 */
		public function set lineScrollSize($value:int):void
		{
			_scrollBar.lineScrollSize = $value;
		}
		
		/**
		 *设置滚动条位置左边右边
		 * <p>默认为右边（right） ,左边left</p>
		 * @param $place
		 * 
		 */
		public function setPlace($place:String = "right"):void
		{
			if($place == RIGHT)
			{
				_scrollBar.x = _width + 2;
				_contain.x = 0;
			}else
			{
				_scrollBar.x = 0;
				_contain.x = _scrollBar.width + 2;
			}
		}
		/**
		 * 设置目标容器 
		 * @param $target
		 * 
		 */
		public function set target($target:Sprite):void
		{
			_target = $target;
			_contain.addChild(_target);
			if(_target.height <= _height)_scrollBar.alpha = 0;
		}
		/**
		 *更新 滚动内容和滚动条滑块的大小及位置
		 * 
		 */
		public function upData():void
		{
			_scrollBar.setScrollProperties(_height,0,_target.height - _height,20);
			if(_target.height <= _height)
				_scrollBar.alpha = 0;
			else
				_scrollBar.alpha = 1;
			_target.y = -(_target.height -_height) * (_scrollBar.scrollPosition /_scrollBar.maxScrollPosition );
		}
		/**
		 * 最顶端
		 * 
		 */
		public function goToStart():void
		{
			_scrollBar.scrollPosition = 0;
			_target.y = 0;
		}
		/**
		 * 最低端 
		 * 
		 */
		public function goToLast():void
		{
			_scrollBar.setScrollPosition(_target.height - _height);
		}
		/**
		 *添加内容  
		 * @param $displayObj
		 * 
		 */
		public function addContainer($displayObj:DisplayObject):void
		{
			if(_target.height != 0)$displayObj.y = _target.height +1 ;
			_target.addChild($displayObj);
		}
		/**
		 * 删除单个显示对象 
		 * @param $displayObj
		 * 
		 */
		public function removeSingleContaine($displayObj:DisplayObject):void
		{
			var index:int = _target.getChildIndex($displayObj);
			var displayObj:DisplayObject;
			var num:int = _target.numChildren;
			for(var i:int = index + 1;i < num;i++)
			{
				displayObj = _target.getChildAt(i);
				displayObj.y -= $displayObj.height;
			}
			_target.removeChild($displayObj);
			$displayObj = null;
			upData();
		}
		
		/**
		 * 销毁 
		 * 
		 */
		public function destroy():void
		{
			if(_contain.contains(_target))
				_contain.removeChild(_target);
			_target = null;
			_contain = null;
			_scrollBar = null;
		}
		
	}
}